package cn.mrlong.recordandplay;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.Properties;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button play, recovery, stop, pause;
    private TextView timeText;
    private RecordUtil recordUtil;
    private String path = "http://file.kuyinyun.com/group1/M00/90/B7/rBBGdFPXJNeAM-nhABeMElAM6bY151.mp3";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        recordUtil = new RecordUtil(new RecordUtil.RecordTimeCallback() {
            @Override
            public void recordTime(String time) {
                timeText.setText(time);
            }

            @Override
            public void noticeMsg(String msg) {
                Toast.makeText(MainActivity.this, "" + msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void recordSucc(String file, String fileRecordTime) {
                timeText.setText(new File(file).getTotalSpace() + "   " + fileRecordTime);
            }
        });
    }

    private void initView() {
        play = findViewById(R.id.play);
        play.setOnClickListener(this);
        recovery = findViewById(R.id.recovery);
        recovery.setOnClickListener(this);
        stop = findViewById(R.id.stop);
        stop.setOnClickListener(this);
        pause = findViewById(R.id.pause);
        pause.setOnClickListener(this);

        Button startR = findViewById(R.id.startR);
        startR.setOnClickListener(this);
        Button stopR = findViewById(R.id.stopR);
        stopR.setOnClickListener(this);
        Button recoveryR = findViewById(R.id.recoveryR);
        recoveryR.setOnClickListener(this);

        timeText = findViewById(R.id.time);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PlayRecordUtil.release();
        recordUtil.stopRecord();
    }

    @Override
    public void onClick(View view) {

        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.mp3";
        switch (view.getId()) {
            //*******************录制类********************
            case R.id.startR:
                recordUtil.startRecord(filePath);
                break;
            case R.id.stopR:
                recordUtil.stopRecord();
                break;
            case R.id.recoveryR:
                recordUtil.resetRecord();
                break;
            //*******************播放类**********************
            //播放
            case R.id.play:
                //播放文件地址
                String filePath2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/等一分钟.mp3";
                PlayRecordUtil.playSound("http://172.16.212.137:8080/mp3/9.mp3");
                break;
            //暂停
            case R.id.pause:
                PlayRecordUtil.pause();
                break;
            //重置
            case R.id.recovery:
                PlayRecordUtil.resume();
                break;
            //停止
            case R.id.stop:
                PlayRecordUtil.release();
                break;
        }
    }
}
